// components/base/datetime-picker/index.js
const componentBehavior = require('../../../behaviors/component');
const { COMPONENT_OPTIONS } = require('../../../constants/common');
const currentYear = new Date().getFullYear();

Component({
    behaviors: [componentBehavior],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        show: {
            type: Boolean,
            value: false,
        },
        closeOnClickOverlay: {
            type: Boolean,
            value: true,
        },
        type: {
            type: String,
            value: 'date',
        },
        value: {
            type: Number,
            optionalTypes: [String],
            value: '',
        },
        minHour: {
            type: Number,
            value: 0,
        },
        maxHour: {
            type: Number,
            value: 23,
        },
        minMinute:  {
            type: Number,
            value: 0,
        },
        maxMinute: {
            type: Number,
            value: 59,
        },
        minDate: {
            type: Number,
            value: new Date(currentYear - 10, 0, 1).getTime(),
        },
        maxDate: {
            type: Number,
            value: new Date(currentYear + 10, 11, 31).getTime(),
        },
        confirmButtonText: {
            type: String,
            value: '确定',
        },
        cancelButtonText: {
            type: String,
            value: '取消',
        },
    },
    data: {
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            } else if (type === 'month') {
                return `${value}月`;
            } else if (type === 'day') {
                return `${value}日`;
            }
            return value;
        },
    },
    methods: {
        handleClose() {
            this.setData({
                show: false,
            });
            // this.triggerEvent('close');
        },
        handleConfirm(e) {
            this.handleClose();
            this.triggerEvent('confirm', e.detail);
        },
    }
})
